angular.module('${menuCode}')
.controller("${widgetCode}Ctrl",function($scope,AppKit){
	$scope.isFirstCard = false;
	$scope.isLastCard = false;
	$scope.cardInfos = {};
	$scope.currrentWidgetCode;
	$scope.selectedWidgetId = -1;
	
	var menuBarId = AppKit.getConfig('MenuBarId');
	var url = "/portal/index?PersonalSetting&actionType=getCardInfos&menuBarId="+menuBarId+"&menuCode=${menuCode}";
	AppKit.addSwipeSupport($scope);
	$scope.initData = function(){
		AppKit.getJsonApi(url).then(function(rspJson){
			$scope.cardInfos = rspJson.data.cardInfos;
			$scope.cardSize = rspJson.data.cardSize; 
		});		
	}
	$scope.initData();
	
	$scope.showPopoverSetting = function($event,widgetCode){
		var cardIndex = parseInt($scope.cardInfos[widgetCode]);
		if (cardIndex == 0){
			$scope.isFirstCard = true;
		}else{
			$scope.isFirstCard = false;
		}
		
		if (cardIndex == ($scope.cardSize-1)){
			$scope.isLastCard = true;
		}else{
			$scope.isLastCard = false;
		}
		
		$scope.currrentWidgetCode = widgetCode;
		AppKit.createPopover("card-setting-popover",$scope,$event);
	};
	
	$scope.moveTopCard = function(){
		var widgetCode = $scope.currrentWidgetCode;
		var url = "/portal/index?PersonalSetting&actionType=moveTopCard&menuBarId="+menuBarId+"&menuCode=${menuCode}&widgetCode="+widgetCode;
		AppKit.getJsonApi(url).then(function(rspJson){
			if (rspJson.data == 'success'){
				window.location.reload();	
			}else{
				console.log("moveTopCard failure !");
			}
		});
	};	
	
	$scope.moveUpCard = function(){
		var widgetCode = $scope.currrentWidgetCode;
		var url = "/portal/index?PersonalSetting&actionType=moveUpCard&menuBarId="+menuBarId+"&menuCode=${menuCode}&widgetCode="+widgetCode;
		AppKit.getJsonApi(url).then(function(rspJson){
			if (rspJson.data == 'success'){
				window.location.reload();	
			}else{
				console.log("moveUpCard failure !");
			}
		});
	};
	
	$scope.moveDownCard = function(){
		var widgetCode = $scope.currrentWidgetCode;
		var url = "/portal/index?PersonalSetting&actionType=moveDownCard&menuBarId="+menuBarId+"&menuCode=${menuCode}&widgetCode="+widgetCode;
		AppKit.getJsonApi(url).then(function(rspJson){
			if (rspJson.data == 'success'){
				window.location.reload();	
			}else{
				console.log("moveDownCard failure !");
			}
		});
	};	
	
	$scope.removeCard = function(){
		var widgetCode = $scope.currrentWidgetCode;
		var url = "/portal/index?PersonalSetting&actionType=removeCard&menuBarId="+menuBarId+"&menuCode=${menuCode}&widgetCode="+widgetCode;
		AppKit.getJsonApi(url).then(function(rspJson){
			if (rspJson.data == 'success'){
				window.location.reload();	
			}else{
				console.log("moveTopCard failure !");
			}
		});
	};
	
	$scope.addCard = function(){
		var widgetId = $scope.selectedWidgetId;
		var url = "/portal/index?PersonalSetting&actionType=addCard&menuBarId="+menuBarId+"&menuCode=${menuCode}&widgetId="+widgetId;
		AppKit.getJsonApi(url).then(function(rspJson){
			if (rspJson.data == 'success'){
				window.location.reload();	
			}else{
				console.log("moveTopCard failure !");
			}
		});
	};	
	
	$scope.showRestCards = function(){
		var menuBarId = AppKit.getConfig("MenuBarId");
		var url = "/portal/index?PersonalSetting&actionType=getSelectCardList&menuBarId="+menuBarId+"&menuCode=${menuCode}";
		AppKit.getJsonApi(url).then(function(response){
			$scope.selectedWidgetId = -1;
			$scope.restWidgets = response.data.datas;
		});		
	}
	
	$scope.selectWidget = function(widgetId){
		$scope.selectedWidgetId = widgetId;
	}	

	$scope.openAddWidgetModal = function(){
		AppKit.createModal("add-widget",$scope);
	}
	
	
	$scope.doSearch = function(){
		$scope.modalTitle = "搜索";
		AppKit.createModal("main-search-modal",$scope);
	}
	
	$scope.loadData = function(){
		var url = '/portal/resource?MobileLatestMessagesDataProvider&actionType=findLatestMessages';
		AppKit.getJsonApi(url).success(function(rspJson){
			$scope.sendMessages = rspJson.sendMessages;
			$scope.receiveMessages = rspJson.receiveMessages;
		});	
	}
	$scope.loadData();
	
	$scope.msg = {"title":"","receiveCode":"","content":"","userName":""};
	
	$scope.resetMsg = function(){
		$scope.msg = {"title":"","receiveCode":"","content":"","userName":""};
	}
	
	$scope.sendMessage = function(){
		var url = "/portal_portlets/index?NoticeReminderList&actionType=mobileSendMessage"
		AppKit.postJsonApi(url,$scope.msg).then(function(response){
			if ("success" == response.data){
				$scope.loadData();
				AppKit.successPopup();
			}else{
				AppKit.errorPopup();
			}
		});
	}
	
	$scope.addAppModal = function(){
		$scope.modalTitle = "待选应用模块";
		AppKit.createModal("tochoose-module-modal",$scope);			
	}
	
	$scope.toChooseModuleData=function(){
		var url = '/portal/index?MobileDataProvider&actionType=findToChooseAppModuleList';
		var promise = AppKit.getJsonApi(url);
		promise.success(function(rspJson){
			$scope.toChooseModuleList = rspJson.toChooseModuleList;
		});
	}
	$scope.doSelectModule = function(){
		$scope.receiveList = [];
		for(var i=0;i < $scope.toChooseModuleList.length;i++){
			var item =  $scope.toChooseModuleList[i];
			if(item.checked){
				$scope.receiveList.push(item);
			}
		}
		var url = "/portal/index?MobileDataProvider&actionType=mobileSaveAppModule&mark=selected";
		AppKit.postJsonApi(url,$scope.receiveList).then(function(response){
			if ("success" == response.data){
				AppKit.successPopup();
			}else{
				AppKit.errorPopup();
			}
		});
	}
});